package com.swm.util;

import com.alibaba.fastjson2.JSONObject;
import com.swm.mapper.StockDayDataMapper;
import com.swm.mapper.StockMapper;
import com.swm.pojo.Stock;
import com.swm.pojo.Stock_Day_Data;
import org.apache.ibatis.session.SqlSession;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 师文明
 * @data 2024/12/23 下午3:11
 **/
public class getStockDayData483 {

    public static int allup;

    public static double sumz;
    public static double sumf;
    public static double sum;

    public static int num0;
    public static int numu;
    public static int numd;
    public static DecimalFormat df = new DecimalFormat("#0.000");
    public static int ban = 0;
    static SqlSession sqlSession = SqlSessionUtil.openSession();
    static StockMapper mapper = sqlSession.getMapper(StockMapper.class);
    static StockDayDataMapper stockDayDataMapper = sqlSession.getMapper(StockDayDataMapper.class);

    private static ArrayList<String> dateList = new ArrayList<String>();
    private static int count=0;
    public static List<Stock> getStcokList() {
        List<Stock> stocks = mapper.selectAllStock();
        return stocks;
    }

    public static void main(String[] args) throws Exception {
        List<Stock_Day_Data> stockDayData = stockDayDataMapper.selectAllStockDayData();
        System.out.println(stockDayData.size());
        if(stockDayData.size()>0){
            ExecutorService executorService = Executors.newFixedThreadPool(10);
            //获取所有股票列表
            List<Stock> stcokList = getStcokList();
            for (Stock stock : stcokList) {
                executorService.execute(() -> {
                    try {
                        getData(stock,stockDayData);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                });
            }
            Thread.sleep(300000);

            double v = 0;
            double vz = 0;
            double vf = 0;
            double v3 = 0;
            double v5 = 0;
            double v10 = 0;
            double v20 = 0;

            if (num0 != 0 && sum != 0) {
                v = sum / num0;
                vz= sumz / numu;
                vf= sumf / numd;
            }


            System.out.println(("一天后涨的次数：" + numu + "  跌的次数：" + numd + "  上涨概率： " + df.format((double) numu * 100 / num0)) + "%  平均涨跌幅：" + df.format(v)+ "%  平均涨幅：" + df.format(vz)+ "%  平均跌幅：" + df.format(vf));
            System.out.println("板数：" + ban);
            System.out.println("中股天数："+dateList.size());
        }

    }

    public static void getData(Stock stock,List<Stock_Day_Data> stockDayData1) throws Exception {
        count++;
        // System.out.println(count);
        // List<Stock_Day_Data> stockDayData = stockDayDataMapper.selectStockDayData(stock.getId());
        List<Stock_Day_Data> stockDayData = new ArrayList<Stock_Day_Data>();
        stockDayData1.forEach(stockDayData2 -> {
            if(stockDayData2.getCode().equals(stock.getCode())){
                stockDayData.add(stockDayData2);
            }
        });
        Collections.sort(stockDayData, new Comparator<Stock_Day_Data>() {
            @Override
            public int compare(Stock_Day_Data o1, Stock_Day_Data o2) {
                return o2.getGetdate().compareTo(o1.getGetdate());
            }
        });
        if (stockDayData.size() >  80) {
            for (int i = 1; i < 21; i++) {
                //昨日涨幅
                double v= stockDayData.get(i).getZdf();
                //前日涨幅
                double v00= stockDayData.get(i+1).getZdf();
                //大前日涨幅
                double v000= stockDayData.get(i+2).getZdf();

                JSONObject lines = getLines(stockDayData, i);
                double f1 = Double.parseDouble(lines.get("f1").toString());
                double te1 = Double.parseDouble(lines.get("te1").toString());
                double tw1 = Double.parseDouble(lines.get("tw1").toString());
                double th1 = Double.parseDouble(lines.get("th1").toString());
                double six1 = Double.parseDouble(lines.get("six1").toString());

                JSONObject lines2 = getLines(stockDayData, i+1);
                double f2 = Double.parseDouble(lines2.get("f1").toString());
                double te2 = Double.parseDouble(lines2.get("te1").toString());
                double tw2 = Double.parseDouble(lines2.get("tw1").toString());
                double th2 = Double.parseDouble(lines2.get("th1").toString());
                double six2 = Double.parseDouble(lines2.get("six1").toString());

                //开盘价
                double k= stockDayData.get(i).getK();

                //收盘价
                double s= stockDayData.get(i).getS();

                //最高价
                double g= stockDayData.get(i).getG();
                //最低价
                double d= stockDayData.get(i).getD();

                double k1= stockDayData.get(i+1).getK();

                //收盘价
                double s1= stockDayData.get(i+1).getS();

                //最高价
                double g1= stockDayData.get(i+1).getG();
                //最低价
                double d1= stockDayData.get(i+1).getD();


                double k2= stockDayData.get(i+2).getK();

                //收盘价
                double s2= stockDayData.get(i+2).getS();
                double k3= stockDayData.get(i+3).getK();

                //收盘价
                double s3= stockDayData.get(i+3).getS();
                double k4= stockDayData.get(i+4).getK();

                //收盘价
                double s4= stockDayData.get(i+4).getS();
                double k5= stockDayData.get(i+5).getK();

                //收盘价
                double s5= stockDayData.get(i+5).getS();


                //当天振幅
                double v6 = (g - d) / k;
                double v7 = (s - k)*100 / k;


                boolean bf = d < f1 && s > f1;
                boolean bte = d < te1 && s > te1;
                boolean btw = d < tw1 && s > tw1;
                boolean bth = d < th1 && s > th1;
                boolean bsix = d < six1 && s > six1;
                String word = "";
                int flag = 0;
                if (bf) {
                    word = word.concat("上穿五日线");
                    flag++;
                }
                if (bte) {
                    word = word.concat("上穿10日线");
                    flag++;
                }
                if (btw) {
                    word = word.concat("上穿20日线");
                    flag++;
                }
                if (bth) {
                    word = word.concat("上穿30日线");
                    flag++;
                }
                if (bsix) {
                    word = word.concat("上穿60日线");
                    flag++;
                }


                boolean price = getPrice(stockDayData, i);
                //收盘与最高差距
                double v0000 = (g - s) * 100 / k;
                double v00000 = (k - d) * 100 / k;

                double v000000 = (s - k) * 100 / k;

                //今日成交额

             /*   boolean price12013 = getPriceredu(stockDayData, i,120,13);
                boolean price1011 = getPriceincre(stockDayData, i,13,11);
                boolean price12012 = getPriceredu(stockDayData, i,120,12);
                boolean price1010 = getPriceincre(stockDayData, i,12,10);
                boolean price12011 = getPriceredu(stockDayData, i,120,11);
                boolean price109 = getPriceincre(stockDayData, i,11,9);
                boolean price12010 = getPriceredu(stockDayData, i,120,10);
                boolean price108 = getPriceincre(stockDayData, i,10,8);
                boolean price1209 = getPriceredu(stockDayData, i,120,9);
                boolean price98 = getPriceincre(stockDayData, i,9,7);
                boolean price1208 = getPriceredu(stockDayData, i,120,8);
                boolean price86 = getPriceincre(stockDayData, i,8,6);
                boolean price1207 = getPriceredu(stockDayData, i,120,7);
                boolean price75 = getPriceincre(stockDayData, i,7,5);*/
                if(v < 9.8 && v > 0){
                    //if((price1207&&price75)||(price1208&&price86)||(price1209&&price98)||(price12010&&price108)||(price12011&&price109)||(price12012&&price1010)||(price12013&&price1011)){
                        if (v0000 < 0.2&&flag >1&&flag<4/*&&v7 > 2&&v7<5*/&&price) {



               /* boolean price1206 = getPriceredu(stockDayData, i,120,6);
                boolean price64 = getPriceincre(stockDayData, i,6,4);*/

                double v2 = (s1 - k5) * 100 / k5;
                //120天底部出现连续五根小阳线，加起来一共10个点左右，第6天突破基本没有上影线
                //if(v0000 < 0.2&&v7>3&&s1>k1&&s2>k2&&s3>k3&&s4>k4&&s5>k5&&price12010&&v2>8&&v2<12){
                //if(price12010&&price108&&v < 9.8 && v > 0){
                    //if(price1205&&price53&&v < 9.8 && v > 0){
                //if (/*v0000 < 0.2&&*/ v < 9.8 && v > 0&&flag >1&&flag<4&&v7 > 2&&v7<5/*&& price120 && v < 9.8 && v > 0 && v0000 < 0.2 && price*/) {
                //if(g>s&&s>k&&k>d&&v0000>1&&v00000>1&&v000000<0.5&& v < 9.8 && v > -9.8&&price120){
                boolean b8 = f1 > te1 && te1 > tw1 && tw1 > th1 && th1 > six1;
                boolean b9 = f2 < six2 || f2 < th2 || f2 < tw2 || f2 < te2 || te2 < six2 || te2 < th2 || te2 < tw2 || tw2 < six2 || tw2 < th2 || th2 < six2;
                double v1 = (s - s1) / s;
                //if(s>k&&s<k1&&(s>s1||v1<0.01)&&s1==d&&g1>s1&&s1<k1&&k<k1){

                    //次日涨幅
                    double v11= stockDayData.get(i-1).getZdf();


                    allup++;
                    if (v11 > 0 || v11 == 0) {
                        numu++;
                        sumz+=v11;
                    } else {
                        sumf+=v11;
                        numd++;
                    }
                    sum += v11;


                    num0++;

                      /*  String date = stockDayData.get(i).getGetdate();
                        if(date.startsWith("2023")){
                            System.out.println(date);
                        }*/
                        if (!dateList.contains(stockDayData.get(i).getGetdate())) {
                        dateList.add(stockDayData.get(i).getGetdate());
                    }
                    if (v11 > 9.8) {
                        ban++;
                    }

                    System.out.println(stockDayData.get(i).getName() + " : " + stockDayData.get(i).getGetdate() + "  当日涨幅" + v + "   次日涨幅" + v11);
                }
            }

            }
        }
    }

    //获取所有均线
    public static JSONObject getLines(List<Stock_Day_Data> stockDayData, int startDay) {
        //日线
        double totalsix1 = 0.0;
        double totalth1 = 0.0;
        double totaltw1 = 0.0;
        double totalte1 = 0.0;
        double totalf1 = 0.0;
        for (int j = 0; j < 60; j++) {
            double price = stockDayData.get(startDay + j).getS();

            if (j < 5) {
                totalf1 += price;
            }
            if (j < 10) {
                totalte1 += price;
            }
            if (j < 20) {
                totaltw1 += price;

            }
            if (j < 30) {
                totalth1 += price;

            }
            totalsix1 += price;

        }
        double six1 = totalsix1 / 60;
        double th1 = totalth1 / 30;
        double tw1 = totaltw1 / 20;
        double te1 = totalte1 / 10;
        double f1 = totalf1 / 5;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("f1", f1);
        jsonObject.put("te1", te1);
        jsonObject.put("tw1", tw1);
        jsonObject.put("th1", th1);
        jsonObject.put("six1", six1);
        return jsonObject;

    }

    //近120天 价格处于低位
    public static boolean getPrice(List<Stock_Day_Data> stockDayData, int startDay) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = stockDayData.get(startDay + 1).getS();
        double s2 = stockDayData.get(startDay).getS();
        for (int j = 0; j < 10; j++) {
            //收盘价
            double p2 = stockDayData.get(startDay + j).getS();
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s2 < prices.get(2)/*s1==prices.get(0)*/;
    }
    //近120天 价格处于低位
    public static boolean getPriceincre(List<Stock_Day_Data> stockDayData, int startDay,int high,int low) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = stockDayData.get(startDay + 1).getS();
        double s2 = stockDayData.get(startDay).getS();
        for (int j = 0; j < high; j++) {
            //收盘价
            double p2 = stockDayData.get(startDay + j).getS();
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s2 > prices.get(low)/*s1==prices.get(0)*/;
    }

    //近120天 价格处于低位
    public static boolean getPriceredu(List<Stock_Day_Data> stockDayData, int startDay,int high,int low) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = stockDayData.get(startDay + 1).getS();
        double s2 = stockDayData.get(startDay).getS();
        for (int j = 0; j < high; j++) {
            //收盘价
            double p2 = stockDayData.get(startDay + j).getS();
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s2 < prices.get(low)/*s1==prices.get(0)*/;
    }
}

